package com.zzw.zim.system.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zzw.zim.system.entitys.SysLogLoginEntity;
import com.zzw.zim.common.utils.AddressUtils;
import com.zzw.zim.common.utils.HttpContextUtils;
import com.zzw.zim.common.utils.IpUtils;
import com.zzw.zim.system.dao.SysLogLoginDao;
import org.springframework.http.HttpHeaders;
import org.springframework.stereotype.Service;
import com.zzw.zim.system.service.SysLogLoginService;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.UUID;


@Service
public class SysLogLoginServiceImpl extends ServiceImpl<SysLogLoginDao, SysLogLoginEntity> implements SysLogLoginService {

    @Override
    public void save (String username, Integer status, Integer operation) {
        HttpServletRequest request = HttpContextUtils.getHttpServletRequest();
        assert request != null;
        String userAgent = request.getHeader(HttpHeaders.USER_AGENT);
        String ip = IpUtils.getIpAddr(request);
        String address = AddressUtils.getAddressByIP(ip);

        SysLogLoginEntity entity = SysLogLoginEntity.builder()
                .id(UUID.randomUUID().toString().replace("-",""))
                .username(username)
                .createTime(new Date())
                .status(status)
                .operation(operation)
                .ip(ip)
                .userAgent(userAgent)
                .address(address)
                .build();
        baseMapper.insert(entity);
    }

}
